package admission.ecnu.utils;

import java.util.Calendar;

public class Utils {

  public enum CollegeYear {
    
    FRESHMAN, SEPHOMORE, JUNIOR, SENIOR;
    
    public static CollegeYear[] getList() {
      return new CollegeYear[]{FRESHMAN, SEPHOMORE, JUNIOR, SENIOR};
    }
  };

  public enum DayHour {
    
    MORN_ONE, MORN_TWO, AFTN_ONE, AFTN_TWO, NIGHT, NONE;
  
    public static DayHour[] getList() {
      return new DayHour[]{MORN_ONE, MORN_TWO, AFTN_ONE, AFTN_TWO, NIGHT};
    }
  };

  
  
  // get DayHour from an exam's start time
  public static DayHour getDayHourFromStartTime(long timeInMillis) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeInMillis);
    return getDayHourFromStartTime(calendar.get(Calendar.HOUR_OF_DAY));
    
  }

  public static DayHour getDayHourFromStartTime(int hourOfDay) {
    switch(hourOfDay) {
    case 8:
      return DayHour.MORN_ONE;
    case 10: 
      return DayHour.MORN_TWO;
    case 13:
      return DayHour.AFTN_ONE;
    case 15:
      return DayHour.AFTN_TWO;
    case 18:
      return DayHour.NIGHT;
    default:
      return DayHour.NONE;
    }
  }

  public static Calendar getCalendar(long timeInMillis) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeInMillis);
    return calendar;
  }

  public static String getDate(long timeInMillis) {
    return getDate(getCalendar(timeInMillis));
  }

  // get date in the form: YEAR + MONTH + DAY_OF_MONTH
  public static String getDate(Calendar calendar) {
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    
    StringBuilder builder = new StringBuilder();
    builder.append(year);
    if (month < 10) {
      builder.append("0" + month);
    } else {
      builder.append(month);
    }
    if (day < 10) {
      builder.append("0" + day);
    } else {
      builder.append(day);
    }
    return builder.toString(); 
  }
  
  
  public static String getDateByOffset(long timeInMillis, int weekOfDay) {
    Calendar calendar = getCalendar(timeInMillis);
    calendar.add(Calendar.DAY_OF_WEEK, weekOfDay - 1); 
    return getDate(calendar);  
  }
  
  public static boolean isSameHalfDay(DayHour dh1, DayHour dh2) {
    if (dh1 == dh2) {
      return true;
    } else if (dh1 == DayHour.MORN_ONE && dh2 == DayHour.MORN_TWO) {
      return true;
    } else if (dh1 == DayHour.MORN_TWO && dh2 == DayHour.MORN_ONE) {
      return true;
    } else if (dh1 == DayHour.AFTN_ONE && dh2 == DayHour.AFTN_TWO) {
      return true;
    } else if (dh1 == DayHour.AFTN_TWO && dh2 == DayHour.AFTN_ONE) {
      return true;
    }
    
    return false;
  } 

  public static boolean isContHalfDay(DayHour dh1, DayHour dh2) {
    if (dh1 == DayHour.MORN_TWO && dh2 == DayHour.AFTN_ONE ) {
      return true;
    }
    if (dh1 == DayHour.AFTN_ONE && dh2 == DayHour.MORN_TWO) {
      return true;
    }
    
    return false;
  }
}
